package com.JustTest;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void main(String[] args) throws IOException {
        new Server().start();
        System.out.println("done");
    }

    public void start() {
        try {

            ServerSocket ss = new ServerSocket(4444);
            while (true) {
                Socket s = ss.accept();
                System.out.println("accept");
                InputStream is = s.getInputStream();
                new Thread(new MyRunnable(is)).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    class MyRunnable implements Runnable {

        InputStream is;

        public MyRunnable(InputStream is) {
            this.is = is;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    if (is.available() > 0) {
                        byte[] b = new byte[is.available()];
                        is.read(b);
                        System.out.println(b.toString());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
